
// 获取全局应用程序实例对象
var app = getApp();
//引入网络库
import { get, post } from "../../util/util.js"

/**
   * 获取网络数据的方法 
   */
var getDataFromServer = function (that) {
  that.setData({
    loading: false,
    hasMore: true
  })
  console.log("剧组数据请求===page" + that.data.page)
  //调用网络请求
  post("/film/baselist", that.data.param).then(data => {
    // if (data != null) {
    if (that.data.page == 1) {
      console.log("剧组数据请求===that.data.page == 1===" + JSON.stringify(data).length)
      that.setData({
        caiItems: data.data,
        loading: true,
        hasMore: false
      })
    } else {
      console.log("剧组数据请求===page>0===chandu" + JSON.stringify(data).length)
      that.setData({
        caiItems: that.data.caiItems.concat(data.data),
        loading: true,
        hasMore: false
      })
    }
    // }
  })
}
// 创建页面实例对象
Page({
  /**
   * 页面名称
   */
  name: "movie",
  /**
   * 页面的初始数据
   */

  data: {
    uid: '',
    showView: false,
    content: [],
    nv: [],
    px: [],
    qyopen: false,
    qyshow: false,
    nzopen: false,
    pxopen: false,
    nzshow: false,
    pxshow: false,
    select1: '',
    select2: '',
    shownavindex: '',
    // selPtStr: '',
    selTcStr: '',
    selPxStr: '',
    selQbStr: '',
    pingtaiDataList: [],
    pingtaiData: '',
    //平台  
    pingtai: [],
    //题材
    ticai: [],
    //
    ticaiDataList: [],
    ticaiData: '',
    order: 'create', //排序方式，create=最新发布，end=即将结束
    filter: 'all',//筛选条件，all=全部，online=正在招募
    //上下拉
    caiItems: [],
    loading: true,
    hasMore: false,
    page: 1,
    scrollTop: 0,
    param: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 生命周期函数--监听页面加载
    showView: (options.showView == "true" ? true : false)
  },

  onLoad: function () {
    var that = this;
    var data = {};
    wx.getStorage({
      key: 'login',
      success: function (res) {
        var data = res.data
        that.setData({
          uid: data.data.uid
        });
      },
    })
    that.setData({
      nv: ['最新发布', '即将结束'],
      px: ['全部', '正在招募']
    })

    that.setData({
      page: 1,
      param: data,
    })
     
    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({
      param: data
    }) 

    getDataFromServer(that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  listQy: function (e) {
    var that = this;
    var data = {};
    //剧组配置信息获取 
    post("/film/createconf", {}).then(data => {
      var jsonArrPt = data.data.play_platform;
      var jsonArrTc = data.data.subject;
      var pingtaiList = [];
      var ticaiList = [];
      var DataList1 = [];
      var DataList2 = [];
      for (var key in jsonArrPt) {
        DataList1.push(key);
        pingtaiList.push(jsonArrPt[key]);
      }
      var pingtaiJson = [];
      for (var i = 0; i < pingtaiList.length; i++) {
        var j = {};
        j.val = DataList1[i];
        j.name = pingtaiList[i];
        j.value = i;
        j.checked = false;
        pingtaiJson.push(j);
      }

      for (var key in jsonArrTc) {
        DataList2.push(key);
        ticaiList.push(jsonArrTc[key]);
      }

      var ticaiJson = [];
      for (var i = 0; i < ticaiList.length; i++) {
        var j = {};
        j.val = DataList1[i];
        j.name = ticaiList[i];
        j.value = i;
        j.checked = false;
        ticaiJson.push(j);
      }

      if (that.data.pingtai.lenth == null) {
        that.setData({
          pingtaiDataList: DataList1,
          ticaiDataList: DataList2,
          pingtai: pingtaiJson,
          ticai: ticaiJson
        });
      }

      if (that.data.qyopen) {
        that.setData({
          qyopen: false,
          nzopen: false,
          pxopen: false,
          nzshow: true,
          pxshow: true,
          qyshow: false,
          shownavindex: 0
        })
      } else {
        that.setData({
          qyopen: true,
          pxopen: false,
          nzopen: false,
          nzshow: true,
          pxshow: true,
          qyshow: false,
          shownavindex: e.currentTarget.dataset.nav
        })
      }
    })

    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({ 
      param: data
    })
  },
  list: function (e) {
    var that = this;
    var data = {};
    if (this.data.nzopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        shownavindex: 0
      })
    } else {
      this.setData({
        content: this.data.nv,
        nzopen: true,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        shownavindex: e.currentTarget.dataset.nav
      })
    }

    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({
      param: data
    })
  },
  listpx: function (e) {
    var that = this;
    var data = {};
    if (this.data.pxopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        shownavindex: 0
      })
    } else {
      this.setData({
        content: this.data.px,
        nzopen: false,
        pxopen: true,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        shownavindex: e.currentTarget.dataset.nav
      })
    }
    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({
      param: data
    })
  },
  selectleft: function (e) {
    var that = this;

    that.setData({
      select2: ''
    });

  },
  selectcenter: function (e) {
    var that = this;
    that.setData({
    });
  },

  /**
   * 获取点击 播出平台 字段
   */
  getPT: function (e) {
    var that = this;
    var data = {};
    var pingtaiShenGao = that.data.pingtai;
    var index = parseInt(e.currentTarget.dataset.index);

    console.log(that.data.pingtai[index].val + "播出平台" + that.data.pingtai[index].checked);
    if (pingtaiShenGao[index].checked) {
      pingtaiShenGao[index].checked = false;
      that.setData({
        pingtaiData: '',
      })
    } else {
      for (var i = 0; i < pingtaiShenGao.length; i++) {
        pingtaiShenGao[i].checked = false;
        that.setData({
          pingtai: pingtaiShenGao
        })
      }
      pingtaiShenGao[index].checked = true;
      that.setData({
        pingtaiData: that.data.pingtaiDataList[index],
      })
    }
 
    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    } 

    that.setData({
      currentItemPt: index,
      pingtai: pingtaiShenGao,
      page: 1,
      param: data
    })

    getDataFromServer(that)
  },

  /**
  * 获取点击 题材 字段
  */
  getTC: function (e) {
    var that = this;
    var data = {};
    var ticaiShenGao = that.data.ticai;
    var index = parseInt(e.currentTarget.dataset.index);

    if (ticaiShenGao[index].checked) {
      ticaiShenGao[index].checked = false;
      that.setData({
        ticaiData: '',
      })
    } else {
      for (var i = 0; i < ticaiShenGao.length; i++) {
        ticaiShenGao[i].checked = false;
        that.setData({
          ticai: ticaiShenGao
        })
      }
      ticaiShenGao[index].checked = true;
      that.setData({
        ticaiData: that.data.ticaiDataList[index],
      })
    }

    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }  

    that.setData({
      ticai: ticaiShenGao,
      currentItemTc: index,
      page: 1,
      param: data
    })

    console.log(that.data.ticai[index].val + "题材" + that.data.ticai[index].checked);
    getDataFromServer(that)
  },

  /**
   * 获取点击 排序
   */
  getPX: function (e) {
    var that = this;
    var data = {};
    var index = parseInt(e.currentTarget.dataset.index);

    if (that.data.nv[index] == '最新发布') {//create
      that.setData({
        order: 'create',
      })
    } else if (that.data.nv[index] == '即将结束') {//end
      that.setData({
        order: 'end',
      })
    }

    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({
      currentItemPx: index,
      selPxStr: that.data.nv[index],
      nzopen: false,
      pxopen: false,
      qyopen: false,
      nzshow: true,
      pxshow: false,
      qyshow: true,
      shownavindex: 0,
      page: 1,
      param: data
    })

    getDataFromServer(that)
  },

  /**
 * 获取点击 全部
 */
  getQB: function (e) {
    var that = this;
    var data = {};
    var index = parseInt(e.currentTarget.dataset.index);

    if (that.data.px[index] == '全部') {
      that.setData({
        filter: 'all',
      })
    } else if (that.data.px[index] == '正在招募') {
      that.setData({
        filter: 'online',
      })
    }

    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({
      currentItemQb: index,
      selQbStr: that.data.px[index],
      nzopen: false,
      pxopen: false,
      qyopen: false,
      nzshow: true,
      pxshow: false,
      qyshow: true,
      param: data,
      shownavindex: 0,
      page: 1
    })

    getDataFromServer(that)
  },

  /**
   * 下拉刷新
   */
  refresh: function () {
    console.log("下拉刷新....")
    var that = this;
    that.onLoad()
  },

  /**
   * 上拉拉加载更多
   */
  loadMore: function () {
    var that = this;
    var data = {};

    that.setData({
      page: ++that.data.page,
    })

    data.page = that.data.page;
    data.pagesize = 5;
    if (that.data.ticaiData != "") {
      data.subject = that.data.ticaiData;
    }
    if (that.data.pingtaiData != "") {
      data.platform = that.data.pingtaiData;
    }
    if (that.data.order != "") {
      data.order = that.data.order;
    }
    if (that.data.filter != "") {
      data.filter = that.data.filter;
    }

    that.setData({
      param: data,
    })

    getDataFromServer(that)
  },

  scroll: function (event) {
    var that = this;
    // 该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    that.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  touchCloseDialog1: function (e) {
    var that = this;
    that.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: false,
      shownavindex: 0
    })
  },
  touchCloseDialog2: function (e) {
    var that = this;
    that.setData({
      nzopen: false,
      pxopen: false,
      qyopen: false,
      nzshow: false,
      pxshow: true,
      qyshow: true,
      shownavindex: 0
    })
  },
  touchCloseDialog3: function (e) {
    var that = this;
    that.setData({
      nzopen: false,
      pxopen: false,
      qyopen: false,
      nzshow: true,
      pxshow: false,
      qyshow: true,
      shownavindex: 0
    })
  }

})



